﻿using System.Windows;
using System.Windows.Controls;
using System.ServiceModel.DomainServices.Client.ApplicationServices;
using System.ComponentModel;

namespace DYSales
{
    public partial class LoginWin : ChildWindow
    {
        loginToken token;
        public LoginWin()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(LoginWin_Loaded);
            token = new loginToken() { userId = string.Empty, pwd = string.Empty };
            this.LayoutRoot.DataContext = token;
        }

        void LoginWin_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.Current.IsRunningOutOfBrowser)
            {
                System.Windows.Browser.HtmlPage.Plugin.Focus();
            }
            tb_userId.Focus();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if ((this.tb_userId.GetBindingExpression(TextBox.TextProperty).DataItem as loginToken).Error == null)
            {
                LoginParameters lp = new LoginParameters(tb_userId.Text, tb_pwd.Password, true, null);
                WebContext.Current.Authentication.Login(lp, (lo) =>
                {
                    if (lo.HasError)
                    {
                        tbk_display.Text = lo.Error.ToString();
                    }
                    else if (lo.LoginSuccess == false)
                    {
                        tbk_display.Text = "验证失败";
                    }
                    else if (lo.LoginSuccess == true)
                    {
                        dm.getDS().GetST(lt =>
                        {
                            WebContext.Current.User.u_登陆时间 = lt.Value;
                        }, null);
                        tbk_display.Text = string.Format("登录成功，当前用户：{0}，所属角色：{1}",
                            WebContext.Current.User.Name,
                            string.Join(",", WebContext.Current.User.Roles));
                        this.DialogResult = true;
                    }
                }, null);
            }
        }

        public void GetUserInfo(string userId)
        {
            WebContext.Current.Authentication.LoadUser(delegate
            {
                if (WebContext.Current.User.IsAuthenticated)
                {
                    tbk_display.Text = string.Format("登录成功，当前用户：{0}，所属角色：{1}",
                        WebContext.Current.User.Name,
                        string.Join(",", WebContext.Current.User.Roles));
                }
            }, null);
        }

        public void logout()
        {
            WebContext.Current.Authentication.Logout((lo) =>
            {
                if (lo.HasError)
                    tbk_display.Text = lo.Error.ToString();
                else
                    tbk_display.Text = "登出成功";
            }, null);
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }

    public class loginToken : IDataErrorInfo
    {
        public string userId { get; set; }
        public string pwd { get; set; }

        public string Error
        {
            get
            {
                if (userId == string.Empty || userId.Contains("#") || pwd == string.Empty)
                {
                    return "error";
                }
                else { return null; }
            }
        }

        public string this[string columnName]
        {
            get
            {
                switch (columnName)
                {
                    case "userId":
                        if (userId == string.Empty)
                        {
                            return "账号不能空";
                        }
                        else if (userId.Contains("#"))
                        {
                            return "账号不能使用#字符";
                        }
                        else
                        {
                            goto default;
                        }
                    case "pwd":
                        if (pwd == string.Empty)
                        {
                            return "密码不能空";
                        }
                        else
                        {
                            goto default;
                        }
                    default:
                        return null;
                }
            }
        }
    }
}

